package util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.apache.catalina.ant.FindLeaksTask;
import org.apache.naming.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class DBUtil {

	static private ClassPathXmlApplicationContext bean = new ClassPathXmlApplicationContext("applicationcontext.xml");
//	static private String url="jdbc:oracle:thin:@localhost:1521:orcl";
//	static private String user = "wx";
//	static private String password = "123";
	
	public static Connection getConn(){
		Connection conn = null;
		DataSource ds = (DataSource)bean.getBean("ds");
		try {
			conn = ds.getConnection();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return conn;
//		Connection conn = null;
//		try {
//			Class.forName("oracle.jdbc.driver.OracleDriver");
//			conn = DriverManager.getConnection(url, user, password);
//		} catch (ClassNotFoundException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (SQLException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		return conn;
	}
	
	public static void free(Connection conn,ResultSet rs, PreparedStatement ps){
		
		try {
			if(conn!=null) conn.close();
			if(rs!=null)  rs.close();
			if(ps!=null) ps.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
